package com.hushow.plugin.tzdemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hushow.plugin.tzdemo.dao.mapper.KctzMapper;
import com.hushow.plugin.tzdemo.service.KctzService;
import com.hushow.plugin.tzdemo.dao.domain.Kctz;
import com.hushow.plugin.tzdemo.vo.request.*;
import com.hushow.plugin.tzdemo.vo.response.*;
import com.hucodex.rest.common.response.PageOutput;
import org.springframework.stereotype.Service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hucodex.rest.common.utils.LoginUtil;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Kctz)表服务实现类
 *
 * @author hushowly
 * @since 2025-06-29 18:14:16
 */
@Service("kctzService")
public class KctzServiceImpl extends ServiceImpl<KctzMapper, Kctz> implements KctzService {

    @Resource
    private LoginUtil loginUtil;

    @Resource
    KctzMapper kctzMapper;

    /**
     * 分页查询
     *
     * @param listReq
     * @return
     */
    public PageOutput<ListKctzByPageResp> listKctzByPage(ListKctzByPageReq listReq) {
        Page<Kctz> page = new Page<>(listReq.getPageNo(), listReq.getPageSize(), true);
        Kctz kctz = BeanUtil.copyProperties(listReq, Kctz.class);
        IPage ipage = this.page(page, new LambdaQueryWrapper<>(kctz).orderByDesc(Kctz::getDumpTime));
        List<ListKctzByPageResp> kctzVoList = null;
        if (!CollectionUtils.isEmpty(ipage.getRecords())) {
            kctzVoList = BeanUtil.copyToList(ipage.getRecords(), ListKctzByPageResp.class);
        }
        return new PageOutput(ipage.getCurrent(), ipage.getSize(), ipage.getTotal(), ipage.getPages(), kctzVoList);
    }

    /**
     * 查询对象
     *
     * @param id
     * @return
     */
    public KctzVo getKctzById(String id) {
        Kctz kctz = this.getById(id);
        if (kctz == null) {
            return null;
        }
        return BeanUtil.copyProperties(kctz, KctzVo.class);
    }


    /**
     * 新增指标
     *
     * @param addReq
     * @return
     */
    public KctzVo addKctz(AddKctzReq addReq) {
        Kctz kctz = BeanUtil.copyProperties(addReq, Kctz.class);
        kctz.setId(IdUtil.simpleUUID());

        this.save(kctz);
        return BeanUtil.copyProperties(kctz, KctzVo.class);
    }

    /**
     * 修改指标
     *
     * @param updateReq
     * @return
     */
    public KctzVo updateKctz(UpdateKctzReq updateReq) {
        Kctz kctz = BeanUtil.copyProperties(updateReq, Kctz.class);
        this.updateById(kctz);
        return BeanUtil.copyProperties(kctz, KctzVo.class);
    }


}

